package fr.cephb.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.zip.GZIPInputStream;

/**
 * This abstract class is used to get a unified way to open and re-open again a {@link java.io.Reader }
 * from a souce (could be a File, a String or a remote URL)
 * @author lindenb
 *
 */
public abstract class InputSource
	{

	/** the a {@link InputSource} is a String */
	public static class STRING extends InputSource
		{
		private String s;
		public STRING(String s)
			{
			this.s=s;
			}
		@Override
		public Reader getReader() throws IOException
			{
			return new StringReader(this.s);
			}
		}
	
	/** the a {@link InputSource} is a File */
	public static class FILE extends InputSource
		{
		private File file;
		public FILE(File file)
			{
			this.file=file;
			}
		public FILE(String file)
			{
			this(new File(file));
			}
		@Override
		public Reader getReader() throws IOException
			{
			try {
				return new InputStreamReader(new GZIPInputStream(new FileInputStream(this.file)));
			} catch (IOException e) {
				return new FileReader(this.file);
			}
			
			
			}
		}
	
	/** the a {@link InputSource} is an URL */
	public static class URL extends InputSource
		{
		private java.net.URL url;
		public URL(java.net.URL url)
			{
			this.url=url;
			}
		@Override
		public Reader getReader() throws IOException
			{
			return new InputStreamReader(this.url.openStream());
			}
		}
	
	/** re-open this source of data and return a java.io.Reader */
	public abstract Reader getReader() throws IOException;
	}
